package com.crazy.dao.sys;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.crazy.entity.sys.PubResource;

/**
 * 资源DAO接口
 * 
 * @author Zhang Weiwei
 * @since 2013-9-9下午12:04:44
 */
public interface PubResourceDao extends BaseDao<PubResource> {
	/**
	 * 按用户主键,启用状态,查询资源类型为menu的资源集合
	 * 
	 * @author Zhang Weiwei
	 * @since 2013-11-3下午4:10:03
	 * @param userId
	 *            用户主键
	 * @param enabled
	 *            启用状态
	 * @param type
	 *            资源类型
	 * @return
	 */
	@Query(value = "SELECT t.* FROM pub_resource t WHERE t.resource_id IN ( SELECT t.resource_id FROM pub_resource t INNER JOIN pub_role_resource t4 ON t.resource_id = t4.resource_id INNER JOIN pub_role t1 ON t4.role_id = t1.role_id INNER JOIN pub_user_role t6 ON t1.role_id = t6.role_id INNER JOIN pub_user t2 ON t6.user_id = t2.user_id WHERE t2.user_id = :userId AND t1.enabled = :enabled AND t.enabled = :enabled AND t.resource_type = :type GROUP BY t.resource_id ) ORDER BY t.priority ASC, t.parent_id ASC", nativeQuery = true)
	List<PubResource> findByUserId(@Param("userId") String userId,
			@Param("enabled") int enabled, @Param("type") String type);

	/**
	 * @param enabled
	 *            启用状态
	 * @return 按启用状态查询资源集合
	 */
	@Query("from PubResource where resourceUrl is not null and enabled=:enabled")
	List<PubResource> findByEnabled(@Param("enabled") int enabled);
}
